//
//  EditSelfTableViewController.m
//  17DianYing
//
//  Created by apple on 15/9/24.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "EditSelfTableViewController.h"
#import "EditSelfManager.h"
#import "SelfViewManager.h"



@interface EditSelfTableViewController ()<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UIButton *headImager;

@property (weak, nonatomic) IBOutlet UITextField *Name;

@property (weak, nonatomic) IBOutlet UIButton *Gender;

@property (nonatomic,strong)NSString *xinbie;

@property(nonatomic,strong) EditSelfManager *editSelfM;

@property(nonatomic,strong)UIImage *headButtonImage;

@property(nonatomic,strong)SelfViewManager *selfViewM;

@property (nonatomic,assign)int count;

@end

@implementation EditSelfTableViewController

- (void)viewDidLoad
{
    
    [super viewDidLoad];
    
    [self.Name becomeFirstResponder];
    
    
    self.count = 1;
    self.headButtonImage = [[UIImage alloc]init];
    
    self.selfViewM = [SelfViewManager share];
    
    self.editSelfM = [EditSelfManager sharManager];
    
    [self setHeadImage];
    
    self.xinbie = [[NSString alloc]init];

    UITapGestureRecognizer *a = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(a:)];
    [self.view addGestureRecognizer:a];
}

-(void)a:(UITapGestureRecognizer*)sender
{
    [self.view endEditing:YES];
}


- (IBAction)TupBaoCunButton:(id)sender
{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *usertoken = [user objectForKey:@"usertoken"];
    
    if (self.count == 1)
    {
        self.xinbie = @"boy";
    }
    else
    {
         self.xinbie = @"girl";
    }
    
    
    
    NSString *getSex = self.xinbie;
    
    [self.editSelfM setSelfWithSex:getSex andNickName:self.Name.text andToken:usertoken andBlock:^(NSDictionary *dic)
    {
        
        NSString *str = [NSString stringWithFormat:@"%@",dic[@"status"]];
        
        if ([str isEqualToString:@"0"])
        {
            UIAlertView *aler = [[UIAlertView alloc]initWithTitle:@"恭喜您" message:@"上传成功" delegate:self cancelButtonTitle:nil otherButtonTitles:@"取消", nil];
            [aler show];
        }
    }];
    
    
}
- (IBAction)TupGenderButton:(id)sender
{
    
    if (self.count == 0)
    {
        [self.Gender setTitle:@"男" forState:0];
        
        [self.Gender setTitleColor:[UIColor cyanColor] forState:0];
        
        self.count++;
    }
    else
    {
        [self.Gender setTitle:@"女" forState:0];
        
        [self.Gender setTitleColor:[UIColor redColor] forState:0];
        
        self.count--;
    }
    
    
}

-(void)setHeadImage
{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *usertoken = [user objectForKey:@"usertoken"];
    
    [self.selfViewM getSelfWithToken:usertoken BlockHoudle:^(NSDictionary *dic) {
        
        
        NSLog(@"************************************%@",dic);
        NSString *str = [NSString stringWithFormat:@"%@",dic[@"nickName"]];
        self.Name.text = str;

        NSString *pcstr = dic[@"pictureUrl"];
        
        if ([dic[@"sex"] isEqualToString:@"boy"])
        {
            self.count = 1;
            
            
        }
        else
        {
            self.count = 0;
            
            [self.Gender setTitle:@"女" forState:0];
            
            [self.Gender setTitleColor:[UIColor redColor] forState:0];
        }
        
        
        NSURL *url = [NSURL URLWithString:pcstr];
        NSLog(@"%@",url);
        UIImage *imgFromUrl =[[UIImage alloc]initWithData:[NSData dataWithContentsOfURL:url]];
        [self.headImager setBackgroundImage:imgFromUrl forState:0];
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)getHeadImager:(NSData *)data;
{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *usertoken = [user objectForKey:@"usertoken"];
    NSString *typstr = @"png";
    
    self.headButtonImage = [UIImage imageNamed:@"123.png"];
    
    

     [self.editSelfM getHeadImagerWithToken:usertoken andData:data andTyp:typstr andBlock:^(NSDictionary *dic) {
         
     }];
        
}
- (IBAction)tupImageButton:(id)sender
{
    UIActionSheet *sheet;
    
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        
    {
        sheet  = [[UIActionSheet alloc] initWithTitle:@"选择" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"拍照",@"从相册选择", nil];
        
    }
    
    else {
        
        sheet = [[UIActionSheet alloc] initWithTitle:@"选择" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"取消" otherButtonTitles:@"从相册选择", nil];
        
    }
    
    sheet.tag = 255;
    
    [sheet showInView:self.view];
}


-(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (actionSheet.tag == 255) {
        
        NSUInteger sourceType = 0;
        
        // 判断是否支持相机
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            
            switch (buttonIndex) {
                case 0:
                    // 取消
                    return;
                case 1:
                    // 相机
                    sourceType = UIImagePickerControllerSourceTypeCamera;
                    break;
                    
                case 2:
                    // 相册
                    sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                    break;
            }
        }
        else {
            if (buttonIndex == 0) {
                
                return;
            } else {
                sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
            }
        }
      
        // 跳转到相机或相册页面
      
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        
        imagePickerController.delegate = self;
        
        imagePickerController.allowsEditing = YES;
        
        imagePickerController.sourceType = sourceType;
        
        [self presentViewController:imagePickerController animated:YES completion:nil];
    }
}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = info[UIImagePickerControllerEditedImage];
    
    self.headButtonImage  = image;
    if (self.headButtonImage == nil)
    {
        return;
    }
    else{
        [self.headImager setBackgroundImage:self.headButtonImage forState:0];
        
        NSLog(@"%@",self.headButtonImage);
        
        NSData *data = UIImageJPEGRepresentation(self.headButtonImage, 0.5);

        
        [self getHeadImager:data];
        
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}



#pragma mark - Table view data source


@end
